document.addEventListener('DOMContentLoaded', function () {
    let playlist = [];
    let currentSongIndex = -1;
    let audioPlayer = document.getElementById('audio-player');
    let playlistContainer = document.getElementById('playlist-container');
    let openPlaylistBtn = document.getElementById('open-playlist');
    let closePlaylistBtn = document.getElementById('close-playlist');
    let songList = document.getElementById('song-list');
    let playlistUL = document.getElementById('playlist');

    // 显示播放列表
    openPlaylistBtn.addEventListener('click', function () {
        playlistContainer.style.display = 'block';
    });

    // 关闭播放列表
    closePlaylistBtn.addEventListener('click', function () {
        playlistContainer.style.display = 'none';
    });

    // 添加歌曲到播放列表
    songList.addEventListener('click', function (event) {
        if (event.target.classList.contains('add-to-playlist')) {
            let songItem = event.target.closest('li');
            let songId = songItem.getAttribute('data-song-id');
            let songName = songItem.querySelector('.song-name').textContent;

            // 将歌曲加入播放列表
            playlist.push({id: songId, name: songName});
            updatePlaylistUI();

            // 播放第一首歌
            if (playlist.length === 1) {
                playSong(0);
            }
        }
    });

    // 更新播放列表的 UI
    function updatePlaylistUI() {
        playlistUL.innerHTML = '';
        playlist.forEach(function (song, index) {
            let li = document.createElement('li');
            li.textContent = song.name;
            li.addEventListener('click', function () {
                playSong(index);
            });

            // 删除歌曲按钮
            let deleteBtn = document.createElement('button');
            deleteBtn.textContent = '删除';
            deleteBtn.addEventListener('click', function () {
                deleteSong(index);
            });
            li.appendChild(deleteBtn);

            playlistUL.appendChild(li);
        });
    }

    // 播放指定歌曲
    function playSong(index) {
        if (index >= 0 && index < playlist.length) {
            currentSongIndex = index;
            let song = playlist[index];
            let audioSource = document.getElementById('audio-source');
            audioSource.src = `songs/${song.id}.mp3`;  // 假设歌曲存放在/songs/目录下
            audioPlayer.load();
            audioPlayer.play();
        }
    }

    // 切换到上一首
    document.getElementById('prev-song').addEventListener('click', function () {
        if (currentSongIndex > 0) {
            playSong(currentSongIndex - 1);
        }
    });

    // 切换到下一首
    document.getElementById('next-song').addEventListener('click', function () {
        if (currentSongIndex < playlist.length - 1) {
            playSong(currentSongIndex + 1);
        }
    });

    // 删除播放列表中的歌曲
    function deleteSong(index) {
        playlist.splice(index, 1);
        updatePlaylistUI();

        // 如果当前正在播放的歌曲被删除，播放下一首
        if (index === currentSongIndex) {
            if (currentSongIndex < playlist.length) {
                playSong(currentSongIndex);
            } else if (playlist.length > 0) {
                playSong(playlist.length - 1);
            } else {
                audioPlayer.pause();
            }
        }
    }
});
$(document).ready(function () {
    // 页面加载时，获取播放歌曲列表
    var context = getContextPath();
    $.ajax({
        url: context + "/play/getPlaySong",
        method: "GET",
        error: function () {
            alert('请求失败，请稍后重试');
        }
    });
});

$(document).ready(function () {
    // 存储已选择的歌曲ID
    var selectedSongIds = [];

    // 监听复选框的变化
    $('.song-checkbox').on('change', function () {
        var songId = $(this).data('song-id');

        // 如果复选框被选中，将歌曲ID添加到数组中，否则移除它
        if ($(this).is(':checked')) {
            if (!selectedSongIds.includes(songId)) {
                selectedSongIds.push(songId);
            }
        } else {
            selectedSongIds = selectedSongIds.filter(id => id !== songId);
        }
    });

    // 监听播放按钮的点击事件
    $('#playBtn').on('click', function () {
        if (selectedSongIds.length === 0) {
            alert('请选择至少一首歌曲');
            return;
        }

        var context = getContextPath();  // 获取项目的上下文路径
        $.ajax({
            //向/play/playMusic发送异步请求
            url: context + '/play/playMusic',
            //改sevlet逻辑是：根据歌曲ID获取歌曲路径以及歌曲名，并查找本项目根目录下/lrc是否有同名的歌词文件，如果有则返回歌词文件路径，没有则返回null，然后重定向至播放详细页面
            method: 'GET',
            data: { songIds: selectedSongIds[0] },  // 目前只能选中的第一首
            success: function(response) {
                // 如果返回的 JSON 中有 redirectUrl 字段，执行跳转
                if (response.redirectUrl) {
                    window.location.href = response.redirectUrl;
                }
            },
            error: function() {
                alert('播放歌曲时发生错误');
            }
        });
    });
});

